package com.matt.service.estimate.impl;

import com.dotflat.staffExtend.po.StaffChildPO;
import com.dotflat.util.excelUtil.ExcelUtil;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.domain.ChildrenNurseryInfoRecord;
import com.matt.domain.estimate.TeacherExtendEstimate;
import com.matt.domain.estimate.vo.TeacherEstimateVO;
import com.matt.foundation.DO.TeacherDO;
import com.matt.foundation.utils.DateUtil;
import com.matt.foundation.vo.WebClassesPageVo;
import com.matt.repository.estimate.TeacherExtendEstimateRepository;
import com.matt.service.estimate.TeacherEstimateService;
import com.matt.service.remoteCall.SsoCallService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestHeader;

import javax.persistence.criteria.Predicate;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import static com.matt.foundation.constant.RequestConstant.TICKET;

@Service
public class TeacherEstimateServiceImpl implements TeacherEstimateService {

    @Autowired
    private TeacherExtendEstimateRepository teacherExtendEstimateRepository;
    @Autowired
    private SsoCallService ssoCallService;

    @Override
    public Page<TeacherExtendEstimate> getPage(String ticket,String teacherName, Integer pageNum, Integer pageSize,String areaId) {
        Assert.notNull(areaId, "园区id不能为空");
        List<TeacherExtendEstimate> all = teacherExtendEstimateRepository.listTeacherEstimateByAreaId(areaId);
        // 教师名称 点评人
        all.forEach(teacherExtendEstimate -> {
            extracted(ticket, teacherExtendEstimate);
        });
        if (StringUtils.isNotEmpty(teacherName)){
            all = all.stream().filter(staff -> staff.getStaffName().contains(teacherName))
                    .collect(Collectors.toList());
        }
        PageImpl<TeacherExtendEstimate> page = new PageImpl<>(all, PageRequest.of(pageNum, pageSize), all.size());
        return page;
    }


    @Override
    public List<TeacherExtendEstimate> getPageList(TeacherExtendEstimate teacherExtendEstimate) {
        if (teacherExtendEstimate==null){
            return null;
        }
        List<TeacherExtendEstimate> all = teacherExtendEstimateRepository
                .listTeacherEstimateByAreaIdAndStaffId(teacherExtendEstimate.getAreaId(),teacherExtendEstimate.getChildrenId());

        return all;
    }

    /**
     * 补充教师姓名和点评人姓名
     * @param ticket
     * @param teacherExtendEstimate
     */
    private void extracted(String ticket, TeacherExtendEstimate teacherExtendEstimate) {
        StaffDTO staff = ssoCallService.getStaffDTO(ticket, teacherExtendEstimate.getStaffId());
        teacherExtendEstimate.setStaffName(staff.getRealName());
//        StaffDTO staff1 = ssoCallService.getStaffDTO(ticket, teacherExtendEstimate.getEvaluatorId());
//        teacherExtendEstimate.setEvaluatorName(staff1.getRealName());
    }

    /**
     * 查询详情
     * @param ticket
     * @param teacherExtendEstimate
     * @return
     */
    @Override
    public TeacherExtendEstimate teacherDetail(String ticket, TeacherExtendEstimate teacherExtendEstimate) {
        Assert.notNull(teacherExtendEstimate.getId(), "id不能为空");
        TeacherExtendEstimate estimate = teacherExtendEstimateRepository.getByTeacherEstimateId(teacherExtendEstimate.getId());
        if (estimate==null){
            return null;
        }

        List<TeacherDO> list = ssoCallService.getTeacherByClassId(teacherExtendEstimate.getId());
        System.out.println(list);

        // 补充姓名
        extracted(ticket,estimate);
        return estimate;
    }

    @Override
    public TeacherExtendEstimate addTeacherEstimate(TeacherExtendEstimate teacherExtendEstimate) {
        Assert.notNull(teacherExtendEstimate.getStaffId(), "教师id不能为空");
        Assert.notNull(teacherExtendEstimate.getTeacherContent(), "评价内容不能为空");
        Assert.notNull(teacherExtendEstimate.getRating(), "评分等级不能为空");

        // TODO 教师不存在则失败
//        StaffDTO staffDTO = ssoCallService.getStaffDTO(ticket);
//        teacherExtendEstimate.setEvaluatorId(staffDTO.getId());

        teacherExtendEstimate.setCreateDate(LocalDate.now().toString());
        teacherExtendEstimateRepository.save(teacherExtendEstimate);
//        teacherExtendEstimateRepository.addTeacherEstimate(teacherExtendEstimate);
        return null;
    }

    @Override
    public TeacherExtendEstimate updateTeacherEstimate(TeacherExtendEstimate teacherExtendEstimate) {
        Assert.notNull(teacherExtendEstimate.getId(), "id不能为空");
        // 评论不存在则返回
        TeacherExtendEstimate estimate = teacherExtendEstimateRepository.findByTeacherEstimateId(teacherExtendEstimate.getId());
        if (Objects.isNull(estimate)) {
            return null;
        }

//        teacherExtendEstimateRepository.updateTeacherEstimate(teacherExtendEstimate.getParentId(), teacherExtendEstimate.getStaffId(),
//                teacherExtendEstimate.getRating(), teacherExtendEstimate.getTeacherContent(), teacherExtendEstimate.getEvaluatorId(), teacherExtendEstimate.getEvaluatorId());
        return teacherExtendEstimateRepository.saveAndFlush(teacherExtendEstimate);
    }

    /**
     * 删除
     * @param teacherExtendEstimate
     * @return
     */
    @Override
    public TeacherExtendEstimate deleteTeacherExtendEstimate(TeacherExtendEstimate teacherExtendEstimate) {
//        Assert.isNull(teacherExtendEstimate.getEvaluatorId(), "评价id不能为空");
        Assert.notNull(teacherExtendEstimate.getId(), "评价id不能为空");
        teacherExtendEstimateRepository.deleteById(teacherExtendEstimate.getId());
        return teacherExtendEstimate;
    }

    @Override
    public void exportTeacherEstimate(HttpServletResponse response,@RequestHeader(TICKET) String ticket,
                                      String teacherName, Integer pageNum, Integer pageSize,String areaId) throws IOException {
        // TODO 导出教师评价

        pageSize=9999;
        pageNum=0;
        List<TeacherEstimateVO> list = new ArrayList<>();
        Page<TeacherExtendEstimate> classCheckList = this.getPage(ticket,teacherName,pageNum,pageSize,areaId);
        ExcelUtil<TeacherEstimateVO> excelUtil = new ExcelUtil<TeacherEstimateVO>(TeacherEstimateVO.class);
        classCheckList.forEach(teacherExtendEstimate -> {
            TeacherEstimateVO teacherEstimateVO = new TeacherEstimateVO();
            teacherEstimateVO.setStaffName(teacherExtendEstimate.getStaffName());
            teacherEstimateVO.setEvaluatorName(teacherExtendEstimate.getEvaluatorName());
            teacherEstimateVO.setRating(teacherExtendEstimate.getRating());
            teacherEstimateVO.setTeacherContent(teacherExtendEstimate.getTeacherContent());
            teacherEstimateVO.setCreateDate(teacherExtendEstimate.getCreateDate());
            list.add(teacherEstimateVO);
        });

        excelUtil.exportExcel(response,list,"教师点评导出","教师点评导出");


    }
}
